home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / bingrab.zip / bingrab.cmd next >
OS/2 REXX Batch file  |  1997-08-08  |  14KB  |  443 lines

  1. /*------------------------------------------------------------------
  2.  * REXX - OS/2 -- BINGRAB ver 0.1a
  3.  * bingrab.cmd : binaries grabber
  4.  * Grabs uudecoded files (multiple and single part) from newsgroups
  5.  * then decodes them into executable binaries.
  6.  * Requires rxsock.dll and uudeview.exe to function. I have included
  7.  * them, but STRONGLY URGE you to go get the complete file from
  8.  * FTP somewhere. They will have documentation on how to setup.
  9.  * The .rc files contain group info to track what groups to grab
  10.  * and also keep track of messages already downloaded.
  11.  * As to the multiple parts - It has it's limitations. You can adjust the
  12.  * minimum size of messages to download to a smaller value to maybe
  13.  * get the final part of a post, but you are also grabbing a bunch
  14.  * of time consuming garbage in the proccess. I have it set to 500.
  15.  * This is mainly because I don't want a bunch of small pictures in my 
  16.  * directory or to spend time downloading files that are not pictures
  17.  * like .html files or text files. I have found that multiple part posts are 
  18.  * missing pieces anyway. The few complete ones that get missed are
  19.  * probably not worth the extra coding it would take to go look for them.
  20.  * Also another important consideration is the number of group names
  21.  * in the binaries.grp file. For each group name listed, a seperate call to
  22.  * BINGRAB.CMD is called and a seperate server connection. This could 
  23.  * lead to a slow machine or even errors. I recommend limiting it to less 
  24.  * than 15, I have it set to 10. You can always create multiple .rc files
  25.  * that contain 10 - 15 names and run them at different times.
  26.  * I have included two utility programs. GROUPS.CMD will get a list of
  27.  * available BINARY groups from the server. This list then can be cut
  28.  * and pasted into the .rc files for BINGRAB.CMD. MOVE.CMD will move all
  29.  * jpg and gif files to a directory named the first characture of the file
  30.  * name. It will then delete any jpg or gif files left in the directory. This 
  31.  * should eliminate any duplicate named files. At this time I have no way
  32.  * to determine if there are duplicate pictures with different names.
  33.  * My experience has been that there are to to many.
  34.  * I hope you like the program. It is EMAILware, so drop me a line if you
  35.  * use it and let me know how you like it and what enhancements you 
  36.  * would like to see. I do not guarantee any further versions, but if any
  37.  * major bugs exist I will release an upgrade.
  38.  * 
  39.  * Thanks, CyberLord. CyberLord@Cyber-Wizard.Com
  40.  *------------------------------------------------------------------*/
  41.  
  42. MINSIZE = 500 /*Minimum size of messages to download*/
  43.  
  44. MAXGROUPS = 10 /*Maximum number of server connections*/
  45.  
  46. parse upper source env how self .
  47. parse arg arguments
  48.  
  49. address cmd
  50. '@ECHO OFF'
  51.  
  52. call syscls
  53. say "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
  54. say "BINGRAB v0.1a Newsgroup Binaries Grabber"
  55. say "by Timothy Millea - CyberLord@Cyber-Wizard.com"
  56. say "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
  57.  
  58. if how = 'FUNCTION' then do
  59.    /* we were started as a function call... */
  60.    /* start us again as a separate session and return. */
  61.    say "Started as a function call"
  62.    say "calling ourselves"
  63.    'START' self arguments
  64.    say "Returning to program that called us"
  65.    return 0
  66. end
  67.  
  68. parse arg server group new .
  69.  
  70. if new > 0 then do
  71.  
  72.   call RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'
  73.   call SysLoadFuncs
  74.  
  75. call syscls
  76. say "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
  77. say "BINGRAB v0.1a Newsgroup Binaries Grabber"
  78. say "by Timothy Millea - CyberLord@Cyber-Wizard.com"
  79. say "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
  80. if (server = "") then
  81.    do
  82.    say "Expecting a news server name to be passed as a parameter."
  83. if (group = "") then
  84.    do
  85.    say "Expecting a newsgroup to be passed as a parameter."
  86.    exit 1
  87.    end
  88.    end
  89.  
  90. if (group = "") then
  91.    do
  92.    say "Expecting a newsgroup to be passed as a parameter."
  93.    exit 1
  94.    end
  95.  
  96. /*------------------------------------------------------------------
  97.  * initialize socket function package
  98.  *------------------------------------------------------------------*/
  99.  
  100.    if RxFuncQuery("SockLoadFuncs") then
  101.       do
  102.       rc = RxFuncAdd("SockLoadFuncs","RxSock","SockLoadFuncs")
  103.       rc = SockLoadFuncs()
  104.       end
  105.  
  106. /*------------------------------------------------------------------
  107.  * get address of server
  108.  *------------------------------------------------------------------*/
  109. rc = SockGetHostByName(server,"host.!")
  110. if (rc = 0) then
  111.    do
  112.    say "Unable to resolve server name" server
  113.    exit
  114.    end
  115.  
  116. server = host.!addr
  117.  
  118. /*------------------------------------------------------------------
  119.  * open socket
  120.  *------------------------------------------------------------------*/
  121. sock = SockSocket("AF_INET","SOCK_STREAM",0)
  122. if (sock = -1) then
  123.    do
  124.    say "Error opening socket:" errno
  125.    exit
  126.    end
  127.  
  128. /*------------------------------------------------------------------
  129.  * connect socket
  130.  *------------------------------------------------------------------*/
  131. server.!family = "AF_INET"
  132. server.!port   = 119
  133. server.!addr   = server
  134.  
  135. rc = SockConnect(sock,"server.!")
  136. if (rc = -1) then
  137.    Error(sock,rc,"Error connecting to newsserver :" errno)
  138.  
  139. trc = GetResponse(sock)
  140. do i = 1 to line.0
  141.    say line.i
  142. end
  143.  
  144. /**************************************************************
  145. Catch breaks
  146. ***************************************************************/
  147. signal on halt
  148.  
  149.  
  150. rc = Interact(sock, group)    /*Jump to interact function*/
  151.  
  152. /*------------------------------------------------------------------
  153.  * quittin' time!
  154.  *------------------------------------------------------------------*/
  155. rc = SendMessage(sock,"quit")
  156. rc = SockSoclose(sock)
  157.  
  158. uudeview.exe "+e .jpg.gif -i" group||".uue" /*decode file*/
  159.  
  160. rc = stream(group||".uue", 'c', 'close')
  161. del group||".uue"
  162.  
  163. exit
  164.  
  165. /*------------------------------------------------------------------
  166.  * Get files from group loop
  167.  *------------------------------------------------------------------*/
  168. Interact:        procedure expose !.
  169.    sock = arg(1)
  170.    group = arg(2)
  171.  
  172. MINSIZE = 500 /*Minimum size of messages to download*/
  173.  
  174. trc = SendMessage(sock,"group" group)
  175. trc = GetResponse(sock)
  176.  
  177. do i = 1 to line.0
  178.          say line.i
  179. end /*do i*/
  180.  
  181. parse var line.1 code total begin end1 .
  182.  
  183. rccommand =  linein(group||'.rc')
  184. parse var rccommand rccode rctotal rcbegin rcend rcgroup
  185.  
  186. if group = rcgroup then do
  187.   begin = rcend
  188.   begin = begin + 1
  189. end /*if*/
  190.  
  191. else if rcgroup != "" then do
  192.   say "Command line "GROUP" does not match with .rc file "RCGROUP
  193.   exit
  194. end /*else if*/
  195.  
  196. say "Last message number read " rcend
  197. say "Newest message number in group " end1
  198.  
  199. if begin > end1 then do 
  200.     say "No new messages in " group
  201.            call lineout group||'.rc', code (rcend-rcend) rcend rcend group, 1
  202.            call lineout group||'.rc'
  203.     return ""
  204. end
  205.  
  206. call lineout group||".uue", group,1
  207. call lineout group||".uue","~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
  208.  
  209.  
  210. call lineout group||'.rc', code (begin-begin) begin begin group, 1
  211. call lineout group||'.rc'
  212.  
  213. number = begin
  214. parse value syscurpos() with row col
  215.    pos = syscurpos(row, col)
  216. say "Getting header number " number "                                       "
  217.  
  218. trc = SendMessage(sock, "head" number)
  219. trc = GetResponse(sock)
  220. do i = 1 to line.0
  221. parse var line.i name lines .
  222. if name = "Lines:" then leave
  223. end /*if*/
  224. if lines > MINSIZE then do
  225.  
  226. pos = syscurpos(row, col)
  227. say "Getting message number " number "                                   "
  228.   trc = SendMessage(sock, "article" number)
  229.   trc = GetResponse(sock)
  230.   say ""
  231.   say "Writing " line.0 "lines.                                                    "
  232.   do i = 1 to line.0
  233.   call lineout group||".uue",line.i
  234.   end /*do i*/
  235.  
  236. call lineout group||'.rc', code (number-begin) begin number group, 1 /*don't want to write number out until message*/
  237. call lineout group||'.rc'                                                                /*is written to .uue file*/
  238.  
  239. end /*if*/
  240.  
  241. else do
  242. call lineout group||'.rc', code (number-begin) begin number group, 1 /*write message number out to file*/
  243. call lineout group||'.rc'                                                                /*after header is read even if less than MINSIZE lines*/
  244.  
  245. end /*else do*/
  246.  
  247. do forever
  248.  
  249. trc = SendMessage(sock, "next")
  250. trc = GetResponse(sock)
  251. parse var line.1 code number .
  252.  
  253. if code = 421 then do /*code 421 is an error that there are no more messages*/
  254. say line.1
  255. call lineout group||'.rc', code (rcend-rcend) rcend rcend group, 1
  256. call lineout group||'.rc'
  257. return ""
  258. end /*if number*/
  259.  
  260. pos = syscurpos(row, col)
  261. say "Getting header number " number "                                            "
  262.  
  263. trc = SendMessage(sock, "head" number)
  264. trc = GetResponse(sock)
  265. do i = 1 to line.0
  266. parse var line.i name lines .
  267. if name = "Lines:" then leave
  268. end /*if*/
  269.  
  270. if lines > MINSIZE then do
  271.  
  272. pos = syscurpos(row, col)
  273. say "Getting message number " number "                                          "
  274.   trc = SendMessage(sock, "article" number)
  275.   trc = GetResponse(sock)
  276.   say ""
  277.   say "Writing " line.0 "lines.                                                            "
  278.   do i = 1 to line.0
  279.   call lineout group||".uue",line.i
  280.   end /*do i*/
  281. call lineout group||'.rc', code (number-begin) begin number group, 1 /*don't want to write number out until message*/
  282. call lineout group||'.rc'                                                                /*is written to .uue file*/
  283.  
  284. end /*if*/
  285.  
  286. else do
  287. call lineout group||'.rc', code (number-begin) begin number group, 1 /*write message number out to file*/
  288. call lineout group||'.rc'                                                                /*after header is read even if less than MINSIZE lines*/
  289.  
  290. end /*else do*/
  291.  
  292. call lineout group||".uue","~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
  293. number = number + 1
  294.         
  295. if number > end1 then do
  296.     say "Finished processing newsgroup"
  297.     call lineout group||".uue"
  298.     leave
  299. end /*if*/
  300. end /*do forever*/
  301. return ""
  302.  
  303. /*------------------------------------------------------------------
  304.  * send a string to the server
  305.  *------------------------------------------------------------------*/
  306. SendMessage:     procedure expose !.
  307.    sock = arg(1)
  308.    data = arg(2) || d2c(13) || d2c(10)
  309.  
  310.    len = length(data)
  311.    do while (len > 0)
  312.       i = SockSend(sock,data);
  313.  
  314.       if (errno <> 0) then
  315.          Error(-1,rc,"Error sending data to server.")
  316.  
  317.       if (i <= 0) then
  318.          Error(sock,100,"Server closed the connection.")
  319.  
  320.       data = substr(data,len+1)
  321.       len  = length(data)
  322.    end
  323.  
  324.    return 0
  325.  
  326.  
  327. /*------------------------------------------------------------------
  328.  * get a response from the server
  329.  *------------------------------------------------------------------*/
  330. GetResponse:     procedure expose !. line.
  331.    sock = arg(1)
  332.  
  333.    moreids = "100 215 220 221 222 223 230 231"
  334.  
  335.    line.0 = 1
  336.    line.1 = GetResponseLine(sock)
  337.  
  338.    parse var line.1 rid .
  339.  
  340.    if (wordpos(rid,moreids) = 0) then
  341.       return ""
  342.  
  343. if rid = 223 then return ""
  344.  
  345.    do forever
  346.       o = line.0 + 1
  347.  
  348.       line.o = GetResponseLine(sock)
  349.  
  350.       if (line.o = ".") then
  351.          return ""
  352.  
  353.       line.0 = o
  354.    end
  355.  
  356.    return ""
  357.  
  358. /*------------------------------------------------------------------
  359.  * get a line from the server
  360.  *------------------------------------------------------------------*/
  361. GetResponseLine: procedure expose !.
  362.    sock = arg(1)
  363.  
  364.    crlf = d2c(13) || d2c(10)
  365.  
  366.    if (symbol('!.buff') = "LIT") then
  367.       !.buff = ""
  368.  
  369.    do while (pos(crlf,!.buff) = 0)
  370.       rc = SockRecv(sock,"data",8000)
  371.       !.buff = !.buff || data
  372.    end
  373.  
  374.    p = pos(crlf,!.buff)
  375.  
  376.    line = substr(!.buff,1,p-1)
  377.    !.buff = substr(!.buff,p+2)
  378.  
  379.    return line
  380.  
  381. /*------------------------------------------------------------------
  382.  * halting ...
  383.  *------------------------------------------------------------------*/
  384. Halting:
  385.    Error(sock,1,"error on line" sigl)
  386.  
  387. /*------------------------------------------------------------------
  388.  * exit with a message and return code
  389.  *------------------------------------------------------------------*/
  390. Error: procedure
  391.    sock = arg(1)
  392.    retc = arg(2)
  393.    msg  = arg(3)
  394.  
  395.    if (sock <> -1) then
  396.       rc = SockSoClose(sock)
  397.  
  398.    say msg
  399.  
  400.    exit retc
  401.  
  402.  
  403. /*------------------------------------------------------------------
  404.  * close socket (and catch signals)
  405.  *------------------------------------------------------------------*/
  406. halt:
  407.  
  408. rc = SendMessage(sock,"quit")
  409. rc = SockSoClose(sock)
  410. if (rc = -1) then
  411.    do
  412.    say "Error on SockSoClose:" errno
  413.    exit
  414.    end
  415.  
  416. uudeview.exe "+e .jpg.gif -i" group||".uue" /*decode file*/
  417.  
  418. rc = stream(group||".uue", 'c', 'close') /*close and delete .uue file*/
  419. del group||".uue"
  420.  
  421. exit
  422.  
  423. end /*if new*/
  424.  
  425. else do
  426. say lines(group)
  427. number = 0
  428. do while lines(group)
  429.     if number < MAXGROUPS then do
  430.     line = linein(group)
  431.     parse var line groupname .
  432.     rc = bingrab(server groupname 1)
  433.     number = number + 1
  434.   end /*if number*/
  435.   else do
  436.     say "Max number of groups reached"
  437.     exit
  438.   end /*else do*/
  439. end /*do while lines*/
  440.  
  441. exit
  442. end /*else do*/
  443.